//
//  EasyRadarView.m
//  scanMoveDemo
//
//  Created by John.lee on 2021/12/23.
//

#import "EasyRadarView.h"

@interface EasyRadarView()
@property(nonatomic,strong)UIView *pointsView;
@property(nonatomic,strong)UILabel *titleLable;
@end
@implementation EasyRadarView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initData];
        [self initView];
    }return self;
}
-(void)initView
{
    [self addSubview:self.titleLable];

}
-(void)initData
{
    self.centerViewRadius = 38;
}

-(void)addPointView
{
    NSInteger arcRectX = arc4random()%205;
    NSInteger arcRectY = arc4random()%205;
    
    UIImageView *pointImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 25,25)];
    pointImage.layer.cornerRadius = 12.5;
    pointImage.layer.masksToBounds = true;
    
    CGRect rect = CGRectMake(arcRectX, arcRectY, 25, 25);
    CGRect iconRect = CGRectMake(170/2, 170/2, 60, 60);
    if (CGRectIntersectsRect(rect, iconRect)) {
        NSLog(@"重叠了-----%@---%@",NSStringFromCGRect(rect),NSStringFromCGRect(iconRect));
        [self addPointView];
        return;
    }
    
    
    pointImage.frame = rect;
    //动画
    pointImage.alpha = 0;
    pointImage.transform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration:1 animations:^{
        pointImage.transform = CGAffineTransformMakeScale(1, 1);
        pointImage.alpha = 1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            pointImage.transform = CGAffineTransformMakeScale(0.1, 0.1);
        } completion:^(BOOL finished) {
            [pointImage removeFromSuperview];
        }];
    }];
    [self.pointsView addSubview:pointImage];

    NSInteger imageIndex = arc4random() % self.pointImages.count;
    pointImage.image = [UIImage imageNamed:self.pointImages[imageIndex]];
//    [pointImage sd_setImageWithURL:[self.pointImages[imageIndex] urlValue] placeholderImage:[UIImage imageNamed:@""]];

    
}
-(void)drawRect:(CGRect)rect
{
    //背景
    [self.bgImage drawInRect:rect];
    
    UIImageView *centerView = [[UIImageView alloc]initWithFrame:CGRectMake(self.center.x - self.centerViewRadius, self.center.y - self.centerViewRadius, 2 * self.centerViewRadius, 2 * self.centerViewRadius)];
    if (self.centerViewImage) {
//        [centerView sd_setImageWithURL:[self.centerViewImage urlValue] placeholderImage:[UIImage imageNamed:@"appointPlaceImage"]];
        centerView.image = [UIImage imageNamed:self.centerViewImage];
    }
    centerView.layer.cornerRadius = self.centerViewRadius;
    centerView.layer.masksToBounds = true;
    [self addSubview:centerView];
    
//    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.offset(0);
//        make.top.equalTo(centerView.mas_bottom).offset(PtOnIPhone6(105));
//    }];
    self.titleLable.frame = CGRectMake(16,CGRectGetMaxY(centerView.frame)+105 , OSScreenWidth - 32, 25);
    
    CALayer *animationLayer = [CALayer layer];
    
    // 利用 for 循环创建三个动画 Layer
    for (int i = 0; i < self.circleIncrement; i++) {
        CABasicAnimation *animation = [self scaleAnimationIndex:i];
        
        // 新建一个动画 Layer，将动画添加上去
        CALayer *pulsingLayer = [self pulsingLayer:rect animation:animation];
        
        //将动画 Layer 添加到 animationLayer
        [animationLayer addSublayer:pulsingLayer];
    }
    
    [self.layer addSublayer:animationLayer];
    
    [self addSubview:self.pointsView];
    self.pointsView.frame = CGRectMake((OSScreenWidth - 230)/2 , self.center.y - 115, 230, 230);
    

}
- (CABasicAnimation *)scaleAnimationIndex:(int)index  {
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = @1;
    scaleAnimation.toValue = @(3.832);
    scaleAnimation.beginTime = CACurrentMediaTime() + (double)(index * 3) / self.circleIncrement;
    scaleAnimation.duration = 3;
    scaleAnimation.repeatCount = HUGE;// 重复次数设置为无限
    return scaleAnimation;
}
- (CALayer *)pulsingLayer:(CGRect)rect animation:(CABasicAnimation *)animation {
    CALayer *pulsingLayer = [CALayer layer];
    pulsingLayer.borderWidth = 0.5;
    pulsingLayer.borderColor = [UIColor redColor].CGColor;
    pulsingLayer.frame = CGRectMake((rect.size.width - 60)/2, (rect.size.height - 60)/2, 60, 60);
    pulsingLayer.cornerRadius = 30;
    [pulsingLayer addAnimation:animation forKey:@"plulsing"];
     
    return pulsingLayer;
}

-(UIView *)pointsView
{
    if (!_pointsView) {
        _pointsView = [[UIView alloc]init];
        _pointsView.backgroundColor = UIColor.clearColor;
    }return _pointsView;
}
-(UILabel *)titleLable
{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc]init];
        NSString *htmlString = @"正在<font color='#0A84FF'>为您匹配</font>老师";
        htmlString = [NSString stringWithFormat:@"<span style='color:#999999'>%@</span>",htmlString];
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
        _titleLable.attributedText = attributedString;
        _titleLable.font = [UIFont systemFontOfSize:14];
        _titleLable.textAlignment = NSTextAlignmentCenter;
    }return _titleLable;
}
@end
